using System;
using System.Linq;
using System.Xml;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Text;
using System.Windows.Browser;
using System.IO.IsolatedStorage;
using System.Windows.Interop;
using System.Windows.Browser.Net;
using System.Net;
using System.Reflection;
using CodePlex.Resourceful.Amazon.S3;
using CodePlex.Resourceful.Amazon.S3.Test;

namespace CodePlex.Resourceful.Core.Example
{
    [CLSCompliant(false)]
    public partial class TestPage : Canvas
    {
        public void Page_Loaded(object o, EventArgs e)
        {
            this.Height = 200;
            this.Width = 200;

            this.Background = new SolidColorBrush(Color.FromArgb(40,200, 200, 200));

            try
            {
                TextBlock output = new TextBlock();
                output.TextWrapping = TextWrapping.Wrap;
                output.SetValue(TextBlock.NameProperty, "Output");
                output.Width = this.Width;
                this.Children.Add(output);

                this.KeyDown += delegate(object sender, KeyboardEventArgs ee)
                {
                    // refresh
                    if (ee.Key == 60)
                    {
                        HtmlPage.Navigate(HtmlPage.DocumentUri.ToString());
                    }
                };


                HtmlPage.Document.GetElementByID("Button1").AttachEvent("onclick", Doit);
                HtmlPage.Document.GetElementByID("Button2").AttachEvent("onclick", Doit);

               
                this.WriteLine(BrowserInformation);
                this.WriteLine("loaded (5)...");
            }
            catch (Exception ex)
            {
                WriteLine(ex.ToString());
            }

          
        }

        private string BrowserInformation
        {
            get
            {
                NameValuePairs nvp = new NameValuePairs();
                nvp["BrowserVersion"] = HtmlPage.BrowserInformation.BrowserVersion;
                nvp["CookiesEnabled"] = HtmlPage.BrowserInformation.CookiesEnabled.ToString();
                nvp["Name"] = HtmlPage.BrowserInformation.Name;
                nvp["Platform"] = HtmlPage.BrowserInformation.Platform;
                nvp["UserAgent"] = HtmlPage.BrowserInformation.UserAgent;
                return Utils.Join("\r\n", nvp);
            }
        }

        private void Doit(object sender, HtmlEventArgs args)
        {

            try
            {

                WriteLine("ID: " + args.SourceElement.ID);
                Uri uri = new Uri(HtmlPage.DocumentUri, "...");

                WriteLine(uri.ToString());


                //Client client = new Client(uri);
                // client.TraceWriter = new SimpleWriter(this.WriteLine);
                //string content = client.GetRepresentation("content.txt").AsString();
                //  WriteLine(content);


                HtmlElement element1 = HtmlPage.Document.GetElementByID("Input1");
                string text1 = element1.GetAttribute("value").Trim();
                HtmlElement element2 = HtmlPage.Document.GetElementByID("Input2");
                string text2 = element2.GetAttribute("value").Trim();

                WriteLine(text1 + ":" + text2);

                HtmlListener listener = new HtmlListener(this.WriteLine);

                Tracer t = new Tracer();
                t.AddListener(listener);


                S3Client s3Client = new S3Client(text1, text2);
                s3Client.Tracer.AddListener(listener);



                S3Connection s3Connection = new S3Connection(text1, text2, false, RequestStyle.Path);
                //s3Connection.Tracer.AddListener(listener);

                Datacenter location = Datacenter.US;
                if (args.SourceElement.ID == "Button1")
                {
                    // S3ClientTests.SanityCheck(s3Client);
                    S3ConnectionTests.Run(t, s3Connection, location);
                }
                else
                {
                    // S3ClientTests.EdgeCaseCheck(s3Client);
                    S3ConnectionTests.Run(t, s3Connection, location);
                }


            }
            catch (Exception ex)
            {
                WriteLine(ex.ToString());
            }

            this.WriteLine("click...");
        }
     

        private void WriteLine(string content)
        {
            content = content.Replace(Environment.NewLine, "<br />");
            HtmlElement element = HtmlPage.Document.GetElementByID("Output");
            HtmlElement msg = HtmlPage.Document.CreateElement("div");
            msg.SetProperty("innerHTML", content);
            element.AppendChild(msg);
        }



        private class HtmlListener : TracerListener {

            private readonly Action<string> _writer;

            public HtmlListener(Action<string> writer)
            {
                _writer = writer;
            }
            public override void WriteLine(string value)
            {
                _writer(HttpUtility.HtmlEncode(value));
            }

            public override void Write(string value)
            {
                _writer(HttpUtility.HtmlEncode(value));
            }
        }

        

    }
}
